/*
 *  Copyright (c) 2014, Peter Haag
 *  Copyright (c) 2009, Peter Haag
 *  Copyright (c) 2008, SWITCH - Teleinformatikdienste fuer Lehre und Forschung
 *  All rights reserved.
 *  
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions are met:
 *  
 *   * Redistributions of source code must retain the above copyright notice, 
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright notice, 
 *     this list of conditions and the following disclaimer in the documentation 
 *     and/or other materials provided with the distribution.
 *   * Neither the name of the author nor the names of its contributors may be 
 *     used to endorse or promote products derived from this software without 
 *     specific prior written permission.
 *  
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 *  POSSIBILITY OF SUCH DAMAGE.
 *  
 *  $Author: haag $
 *
 *  $Id: collector_inline.c 37 2009-11-10 08:40:30Z haag $
 *
 *  $LastChangedRevision: 37 $
 *	
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <time.h>
#include <sys/time.h>
#include <netdb.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/mman.h>
#include <string.h>
#include <dirent.h>


#include "util.h"
#include "nffile.h"
#include "nfx.h"
#include "nf_common.h"
#include "bookkeeper.h"
#include "collector.h"
#include "exporter.h"


extern FlowSource_t *FlowSource;;
static FlowSource_t *GetFlowSource(uint8_t *ipaddr) {
	FlowSource_t *fs;
	ip_addr_t ip;
	
	ip.v6[0] = *((uint64_t *)ipaddr);
	ip.v6[1] = *((uint64_t *)ipaddr + 1);

	fs = FlowSource;
	while ( fs ) {
		if ( ip.v6[0] ==  fs->ip.v6[0] && ip.v6[1] == fs->ip.v6[1] )
			return fs; 

		// if we match any source, store the current IP address - works as faster cache next time
		// and identifies the current source by IP
		if ( fs->any_source ) {
			fs->ip = ip;
			fs->sa_family = AF_INET;
			return fs;
		}
		fs = fs->next;
	}

	LogError("Unknown flow source: %d.%d.%d.%d" , (ip.v4>>24)&0xff, (ip.v4>>16)&0xff, (ip.v4>>8)&0xff, (ip.v4)&0xff);

	return NULL;

} // End of GetFlowSource



